const api = require("../../utils/interface.js")
const math = require("../../utils/math.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
    isShow: false,
    plateNumber: "", //金额
    keyBoardType: 1,
    oilShow: false,
    flag: false,
    moneyList: [100, 200, 300],
    currentIndex: null,
    storeId: "", //门店id
    oilnumber: "", //油号
    oilnum: [], //油号集合
    posnumber: "", //设备号
    posnum: [], //设备号集合
    posId: "", //设备id
    priceStandard: "", //国标价
    price: "", //平台价
    price_:"",
    storeName: "", //油站名称
    address: "", //油站地址
    total: 0, //总计
    productInfoList: [], //产品列表
    productId: "", //产品id 1 柴油 2汽油
    productInfoId: "", //油品详情id
    activemessage: "", //有无活动
    mitoken: "", //幂等性token
    productInfo: [], //当前门店的活动信息
    rise: "", //加油升数
    couponmoney: 0, //直降优惠
    jianmoney: "", //满减优惠
    zhemoney: "", //折扣优惠
    couponList: [], //优惠券信息
    orderNumber: "", //订单编号
    idx: 0, //默认选中的id
    butie: "", //补贴价
    selectCouponList: [],
    couponState: false, //有优惠券时的状态
    bottomMoney: 0, //优惠券金额
    couponmoney1: 0, //优惠金额
    couponIds: [], //优惠券的i
    // by dabai
    couponPageMoney: "",
    superposition: "", // 活动是否与优惠券共享 1 共享 0 不共享
    getcoupnPrevent: 0,
    isIphoneX: false,
    params: "",
    activitySuperposition: "", // 记录活动的是否可与优惠券叠加的状态
    serviceCardDTOList: [],
    couponIdsReceive: [],
    activityId: "",
    couponStatus: null, //提示送优惠券的状态
    serviceCardnum: 0,
    couponnum: 0,
    companyId: "",
    buttonStatus: "", //出现油品选择点击事件
    scene: "",
    bok: true, // 支付开关
    pId: '',
    userInfo:null
  },

  onLoad: function(options) {
    console.log(options)
      wx.showLoading({
        title: '加载中',
        mask: true
      });
      this.data.userInfo = wx.getStorageSync('userInfo')
      getApp().globalData.fundebug.notify("入参", JSON.stringify(options));
      getApp().login().then(() => {
        if (options.storeid) {
          this.data.scene = 'storeid=' + options.storeid
          this.setData({
            storeId: options.storeid,
            oilShow: true,
            buttonStatus: 1
          })
        } else {
          this.data.scene = options.scene
          this.data.params = options.scene.split('_')
          this.data.storeId = this.data.params[1]
          this.setData({
            buttonStatus: 2
          })
        }
        let that = this
        let memberId = this.data.userInfo ? this.data.userInfo.id : ""
        wx.getLocation({
          type: 'wgs84',
          success(res) {
            api.getStoreDistance({
              storeId: that.data.storeId,
              lat: res.latitude,
              lot: res.longitude
            }).then(res => {
              if (res.data.result && res.data.result.distance / 1000 > 2) {
                wx.showModal({
                  title: '提示',
                  content: '当前您的位置距离该加油站超过二公里，是否继续加油',
                  showCancel: false,
                })
              }
            })
            that.oilstation(that.data.storeId, memberId, res.latitude, res.longitude)
          },
          fail() {
            wx.hideLoading()
            wx.getSetting({
              success(res) {
                console.log(res)
                if (res.authSetting["scope.userLocation"] == false) {
                  wx.showModal({
                    title: '提示',
                    content: '你的位置信息将用于小程序位置接口的效果展示',
                    showCancel: false,
                    success(res) {
                      if (res.confirm) {
                        wx.openSetting({
                          success(res) {
                            console.log(res)
                          }
                        })
                      }
                    }
                  })
                } else {
                  wx.showModal({
                    title: '提示',
                    content: '定位失败，请检查手机位置/定位是否打开',
                    showCancel: false
                  })
                }
              }
            })
          }
        })
        let isIphoneX = getApp().globalData.isIphoneX
        this.setData({
          isIphoneX: isIphoneX
        })
      })
  },
  //获取油站信息获取
  oilstation(storeId, memberId, lat, lot) {
    if (storeId == 1) {
      wx.showModal({
        title: '提示',
        content: '该站点消费暂不可用，请联系加油员重新扫码',
      })
    }
    let that = this
    api.getStoreForPay({
      id: storeId,
      memberId: memberId,
      lat: lat,
      lot: lot
    }).then(res => {
      if (res.data.status == 200) {
        const storeName = res.data.result.name
        const address = res.data.result.address
        const productInfo = res.data.result.productInfoStoreDTOList
        const terminalDTOList = res.data.result.terminalDTOList
        this.setData({
          companyId: res.data.result.companyId
        })
        console.log(terminalDTOList, "油站返回的设备列表")
        // 设备
        if (terminalDTOList && terminalDTOList.length && this.data.params) {
          terminalDTOList.map((item, idx) => {
            if (item.id == this.data.params[2]) {
              that.setData({
                posnumber: item.terminalSortName,
                posId: item.id
              })
            }
          })
          console.log(this.data.posId, "posId,从设备列表里面拿")
          getApp().globalData.fundebug.notify("posId,从设备列表里面拿", String(this.data.posId));
        }
        if (!this.data.posId && this.data.params) {
          getApp().globalData.fundebug.notify("手持终端设备id不存在", String(this.data.posId) + '____' + JSON.stringify(terminalDTOList));
        }
        // 油品号
        if (productInfo && productInfo.length && this.data.params) {
          productInfo.map((item, idx) => {
            if (item.productInfoId == this.data.params[3]) {
              console.log(item.productInfoId)
              that.setData({
                oilnumber: item.productInfoName,
                productInfoId: item.productInfoId,
                productId: item.productId,
                price: math.divide(item.plateformPrice, 100),
                priceStandard: math.divide(item.price, 100),
                price_: math.divide(item.plateformPrice, 100)
              })
            }
          })
        }
        that.setData({
          storeName: storeName,
          address: address,
          oilnum: productInfo,
          posnum: terminalDTOList,
          productInfo: productInfo,
        })
        wx.hideLoading();
      } else {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '获取油站信息失败',
          success: function() {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        })
      }
    })
  },
  //获取直降金额 直降金额如果是普通用户那么直降金额就等于油号的国际价减去平台的价格
  //并且activeKind是9的
  //失去焦点时触发键盘
  numClick(e) {
    console.log(e)
    let that = this
    let value = e.detail.value
    wx.pageScrollTo({
      scrollTop: 500,
      duration: 300
    });
    this.setData({
      flag: true,
      isShow: true,
      oilShow: false,
      total: value,
      currentIndex: null
    })
  },
  //点击取消时隐藏键盘
  exit() {
    this.getActivityDetails(this.data.plateNumber)
    this.setData({
      isShow: false,
      oilShow: false,
      couponPageMoney: "",
      couponState: false,
      bottomMoney: "",
      currentIndex: null,
      selectCouponList: []
    })
  },
  //点击键盘删除
  inputdelete(e) {
    if (this.data.plateNumber) {
      const Number = String(this.data.plateNumber).substring(0, String(this.data.plateNumber).length - 1)
      this.setData({
        plateNumber: Number,
      })
      const rise = math.divide(this.data.plateNumber, this.data.priceStandard).toFixed(2) //算出升数
      const money = math.times(this.data.priceStandard, rise) //国际总价
      const couponmoney = math.times(this.data.butie, rise).toFixed(2) //优惠的价格
      this.setData({
        rise: rise,
        couponmoney: 0,
        total: Number,
        bottomMoney: 0,
        couponIds: ""
      })
    }
  },
  //点击键盘数字
  inputChange(e) {
    if (!e.detail) {
      return false
    }
    if (this.data.plateNumber.length >= 8) {
      return false
    }
    this.data.plateNumber += e.detail
    console.log(this.data.plateNumber, "plateNum")
    if (/^(\d?)+(\.\d{0,2})?$/.test(this.data.plateNumber)) { //正则验证，提现金额小数点后不能大于两位数字
      this.setData({
        plateNumber: this.data.plateNumber,
        total: this.data.plateNumber,
        currentIndex: null,
        rise: math.divide(this.data.plateNumber, this.data.priceStandard).toFixed(2),
      })
    } else {
      this.data.plateNumber = String(this.data.plateNumber).substring(0, String(this.data.plateNumber).length - 1)
      this.setData({
        plateNumber: this.data.plateNumber,
        total: this.data.plateNumber,
        currentIndex: null,
        rise: math.divide(this.data.plateNumber, this.data.priceStandard).toFixed(2),
      })
      return false
    }
  },
  //点击.的时候的判断
  click(e) {
    const dian = e.detail;
    const len = this.data.plateNumber.length;
    const index_point = String(this.data.plateNumber).indexOf('.');
    if (index_point === -1) {
      this.data.plateNumber += e.detail
    }
    if (len == 0) {
      this.data.plateNumber = '0.'
    }
    this.data.plateNumber = this.data.plateNumber.replace(' ', '');
    this.setData({
      plateNumber: this.data.plateNumber,
    })
  },
  //点击键盘确认
  inputOk() {
    const total = math.minus(this.data.plateNumber, this.data.couponmoney).toFixed(2)
    this.setData({
      isShow: false,
      total: total,
      couponPageMoney: "",
      couponState: false,
      bottomMoney: "",
      currentIndex: null,
      selectCouponList: []
    })
    return new Promise((res, rej) => {
      this.getActivityDetails(this.data.plateNumber, res)
    })

  },
  //点击键盘确认
  //点击返回
  go_back() {
    wx.navigateBack({
      delta: 1
    })
  },
  //封装更新输入金额时升数和直降优惠的变化
  couponChange() {
    const rise = math.divide(this.data.plateNumber, this.data.priceStandard).toFixed(2) //算出升数
    const money = math.times(this.data.priceStandard, rise) //国际总价
    const couponmoney = math.times(rise, this.data.butie).toFixed(2) //优惠的价格
    const total = math.minus(this.data.total, couponmoney).toFixed(2)
    this.setData({
      rise: rise,
      couponmoney: couponmoney,
      total: total
    })
  },
  //点击油品信息出现弹窗
  showOil() {
    this.setData({
      oilShow: true,
      isShow: false
    })
  },
  //键盘取消
  exit1() {
    console.log('取消')
    this.setData({
      oilShow: false,
      isShow: false,
      total: this.data.plateNumber,
    })
    this.couponChange()
  },
  //跳转到优惠券
  onBtnClick() {
    console.log(this.data.couponList, "coupon")
    let memberId = this.data.userInfo ? this.data.userInfo.id : ""
    // const money = math.times(this.data.total, 100)
    let money = math.minus(this.data.total, this.data.couponmoney)

    let data = {
      orderAmount: math.times(this.data.plateNumber, 100),
      memberId: memberId,
      orderSource: "1",
      // productInfoId: 30,
      productId: this.data.productId,
      storeId: this.data.storeId,
      payType: 2,
      couponmoney: math.times(this.data.couponmoney, 100),
      price: math.times(money, 100),
      superposition: this.data.superposition
    }
    wx.navigateTo({
      url: '../coupon/coupon?couponList=' + JSON.stringify(data) + '&superposition=' + this.data.superposition
    })
    //清空列表
    if (this.data.couponPageMoney) {

      this.data.bottomMoney = math.minus(this.data.bottomMoney, this.data.couponPageMoney)
      this.data.total = math.minus(this.data.plateNumber, this.data.bottomMoney)
      this.setData({
        total: this.data.total,
        bottomMoney: this.data.bottomMoney,
        couponPageMoney: "",
        couponIds: ""
      })
    }

  },
  //点击时金额时
  onItemClick(event) {
    //获取传入的index
    const index = event.currentTarget.dataset.index
    const item = event.currentTarget.dataset.item //传入的金额
    const rise = math.divide(item, this.data.priceStandard).toFixed(2) //算出升数

    this.setData({
      currentIndex: index,
      plateNumber: item,
      rise: rise,
      couponState: false,
      couponPageMoney: "",
      couponIds: "",
      selectCouponList: []
    })
    this.getActivityDetails(item)
  },
  //子组件触发的点击更新油号信息油号id 以及产品id
  oilClick: function(e) {
    if (this.data.buttonStatus == 1) {
      console.log("sadfsadfsdaf")
      let that = this
      const val = e.detail.oilitem
      const val2 = e.detail.productInfoId
      const val4 = e.detail.productId
      for (var i = 0; i < this.data.productInfo.length; i++) {
        if (this.data.productInfo[i].productInfoId == val2) {
          const money = math.divide(this.data.productInfo[i].plateformPrice, 100) //平台价
          this.data.price_ = money
          const priceStandard = math.divide(this.data.productInfo[i].price, 100) //门店价
          const rise = math.divide(this.data.plateNumber, priceStandard).toFixed(2) //加油升数
          const butie = math.divide(this.data.productInfo[i].cutPrice, 100)
          const couponmoney = math.minus(rise, butie).toFixed(2) //直降价格
          that.setData({
            price: money,
            rise: rise,
            oilnumber: val,
            productId: val4,
            productInfoId: val2,
            priceStandard: priceStandard,
            couponIds: "",
            couponPageMoney: ""
          })
          this.getActivityDetails(this.data.plateNumber)
          break;
        }
      }
    }
  },
  //保留设备号及id
  oilClick1: function(e) {
    if (this.data.buttonStatus == 1) {
      console.log(e, "设备信息")
      let that = this
      console.log(this.data.productInfo, "油号和id", val3)
      const val1 = e.detail.pos
      const val3 = e.detail.idx
      that.setData({
        posnumber: val1,
        posId: val3,
      })
      console.log(this.data.posId, "posid--从组件里面获取")
      getApp().globalData.fundebug.notify("posid--从组件里面获取", this.data.posId);
    }

  },
  //点击确定时
  sure(e) {
    if (this.data.plateNumber) {
      this.getActivityDetails(this.data.plateNumber)
    }
    this.setData({
      bottomMoney: "",
    })
  },

  //获取活动信息
  getActivityDetails(item, result) {
    result = result || function() {}
    this.data.serviceCardDTOList = []
    this.data.couponIdsReceive = []
    this.data.activityId = ""
    let openId = wx.getStorageSync("openid")
    let memberId = this.data.userInfo ? this.data.userInfo.id : ""
    let plateNumber = Number(this.data.plateNumber)
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    if (!that.data.productId || !that.data.productInfoId){
      that.setData({
        plateNumber:0,
        total:0
      })
      return false
    }
    api.getActivityDetails({
      activityType: "3",
      amount: math.times(item, 100),
      memberId: memberId,
      orderSource: "1",
      productInfoId: that.data.productInfoId,
      productId: that.data.productId,
      storeId: that.data.storeId,
      payType: 2,
    }).then(res => {
      // 只有是会员角色才能获取优惠券
      if(this.data.userInfo){
        api.getCoupon({
          orderAmount: item * 100,
          memberId: memberId,
          orderSource: "1",
          productId: this.data.productId,
          storeId: this.data.storeId,
          payType: 2,
          couponmoney: this.data.couponmoney * 100,
          price: math.minus(plateNumber, this.data.couponmoney).toFixed(2) * 100
        }).then(res => {
          if (res.data.result.length > 0) {
            this.setData({
              couponState: true
            })
          } else {
            this.setData({
              couponState: false
            })
          }
        })
      }
      if (res.data.status == 200) {
        this.data.activitySuperposition = res.data.result[0].superposition
        if (res.data.result.length > 0) {
          //直降
          if (res.data.result[0].activityKind == "9") {
            const rise = math.divide(this.data.plateNumber, this.data.priceStandard).toFixed(2) //算出升数
            const butie = math.divide(res.data.result[0].activityValue, 100)
            // 直降 = 加油升数*直降金额
            const couponmoney = math.times(rise, butie).toFixed(2)
            // 实付金额 = 订单金额 - 直降金额
            let total = math.minus(this.data.plateNumber, couponmoney)
            this.setData({
              couponmoney: couponmoney,
              bottomMoney: couponmoney,
              total: total,
              zhemoney: 0,
              jianmoney: 0,
              rise: rise,
              price:this.data.price_

            })
          }
          //折扣
          if (res.data.result[0].activityKind == "1") {
            const butie = math.divide(Number(res.data.result[0].activityValue), 1000)
            let price1 = math.times(this.data.plateNumber, butie) // 折扣金额 = 订单金额 * 折扣
            let couponmoney = math.minus(this.data.plateNumber, price1).toFixed(2)
            let total = math.minus(this.data.plateNumber, couponmoney) // 实付金额 = 订单金额 - 折扣金额
            let price = math.times(this.data.priceStandard,math.divide(res.data.result[0].activityValue, 1000))
            this.setData({
              zhemoney: couponmoney,
              bottomMoney: couponmoney,
              couponmoney: 0,
              total: total,
              jianmoney: 0,
              price: this.data.priceStandard
            })
          }
          //满减
          if (res.data.result[0].activityKind == "2") {
            const rise = math.divide(this.data.plateNumber, this.data.priceStandard).toFixed(2) //算出升数
            const butie = math.divide(res.data.result[0].activityValue, 100)
            // 直降 = 加油升数*直降金额
            // const couponmoney = math.times(rise, butie).toFixed(2)
            // 实付金额 = 订单金额 - 满减金额
            let total = math.minus(this.data.plateNumber, butie)
            this.setData({
              couponmoney: 0,
              bottomMoney: butie,
              jianmoney: butie,
              total: total,
              zhemoney: 0,
              rise: rise,
              price: this.data.priceStandard
            })
          }
          //优惠券
          if (res.data.result[0].activityKind == "4") {
            this.setData({
              total: this.data.plateNumber,
              bottomMoney: 0,
              couponmoney: 0,
              zhemoney: 0,
              jianmoney: 0
            })
          }
          // 赠送的增值服务卡
          if (res.data.result[0].serviceCardDTOList && res.data.result[0].serviceCardDTOList.length) {
            res.data.result[0].serviceCardDTOList.map(item => {
              this.data.serviceCardDTOList.push(item.id)
            })
            console.log(this.data.serviceCardDTOList.length, "length")
            this.setData({
              serviceCardnum: this.data.serviceCardDTOList.length
            })
          } else {
            this.setData({
              serviceCardnum: 0
            })
          }
          if (res.data.result[0].couponList && res.data.result[0].couponList.length > 0) {
            res.data.result[0].couponList.map(item => {
              this.data.couponIdsReceive.push(item.id)
            })
            console.log(res.data.result[0].couponList.length, "length")
            this.setData({
              couponnum: res.data.result[0].couponList.length
            })
          } else {
            this.setData({
              couponnum: 0
            })
          }
          if (res.data.result[0].activityId && res.data.result.length) {
            this.data.activityId = res.data.result[0].activityId
          }
        }
        that.setData({
          couponList: res.data.result[0].couponList,
          superposition: res.data.result[0].superposition,
        })
        result()
      } else {
        this.setData({
          zhemoney: 0,
          bottomMoney: 0,
          couponmoney: 0,
          jianmoney: 0,
          total: this.data.plateNumber
        })
        result()
      }
      wx.hideLoading()
    })
  },
  //提交订单
  pay() {
    console.log(this.data.total, "11")
    if (!this.data.bok) {
      return false
    }
    this.data.bok = false
    let that = this
    wx.showLoading({
      title: '支付中',
      mask: true
    })
    if (this.data.isShow) {
      this.inputOk().then(res => {
        console.log(res)
        payMethod()
      })
    } else {
      payMethod()
    }
    // wx.hideLoading()

    function payMethod() {
      let memberId = that.data.userInfo ? that.data.userInfo.id : ""
      let openId = wx.getStorageSync("openid")
      let storeId = Number(that.data.storeId)
      let wxopenid = wx.getStorageSync('openid')
      let money = math.times(that.data.total, 100) //订单金额
      let finalmoney = math.minus(that.data.total, that.data.bottomMoney).toFixed(2) //最终金额
      let plateNumber = Number(that.data.plateNumber)
      //产品id
      //产品型号id
      //设备id
      //支付金额
      //订单金额
      //支付方式
      //订单状态
      //活动减额
      //订单来源
      //加油升数
      //获取的优惠券
      //创建消费订单判断相应的条件
      if (that.data.total <= 0) {
        wx.showModal({
          title: '提示',
          content: '请输入正确的加油金额',
          showCancel: false,
        })
        wx.hideLoading()
        that.data.bok = true
        return false
      }
      getApp().globalData.fundebug.notify("创建订单--posid", String(that.data.posId));
      if (that.data.params && that.data.posId != that.data.params[2]) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '设备错误,请重新扫码',
          showCancel: true
        })
        that.data.bok = true
        return false
      }
      api.createOrder({
        payAmount: math.times(that.data.total, 100),
        openId: that.data.userInfo?'':openId,
        orderAmount: math.times(plateNumber, 100),
        memberId: memberId ,
        oilNum: that.data.oilnumber,
        orderSource: "1",
        productInfoId: that.data.productInfoId,
        productId: that.data.productId,
        terminalId: that.data.posId,
        storeId: storeId,
        companyId: that.data.companyId,
        activityReduction: math.times(that.data.bottomMoney, 100), //活动减额
        downAmount: math.times(that.data.couponmoney, 100), //直降
        couponIds: that.data.couponIds.toString(),
        rebateAmount: math.times(that.data.zhemoney, 100), //折扣优惠
        rise: that.data.rise,
        fullReductionAmount: math.times(that.data.jianmoney, 100), //满减金额
        payType: "2",
        oilPrice: math.minus(math.times(that.data.priceStandard, 100), that.data.butie), //支付的金额
        serviceCardIds: that.data.serviceCardDTOList.toString(),
        couponIdsReceive: that.data.couponIdsReceive.toString(),
        activityId: that.data.activityId
      }, that.data.mitoken).then(res => {
        //预支付接口判断输入选择的油号
        if (res.data.status == 200) {
          console.log(res, "订单信息")
          that.setData({
            orderNumber: res.data.result.orderNumber
          })
          const number = res.data.result.orderNumber
          console.log(that.data.posId, '预支付信息posId')
          getApp().globalData.fundebug.notify("预支付信息posId", String(that.data.posId));
          api.pay({
            payAmount: math.times(that.data.total, 100),
            orderNumber: number,
            memberId: memberId,
            wxOpenId: wxopenid,
            terminalId: that.data.posId,
            storeId: storeId
          }, that.data.mitoken).then(res => {
            console.log(res, "预支付信息")
            const timeStamp = res.data.result.timeStamp
            const nonceStr = res.data.result.nonceStr
            const package_str = res.data.result.package_str
            const signType = res.data.result.signType
            const paySign = res.data.result.paySign
            //预支付成功之后调用微信支付接口
            getApp().copyCityId()
            that.pays2(res.data.result)
          })
        } else {
          console.log(res.data, "创建订单失败的情况")
          wx.hideLoading()
          that.data.bok = true
          wx.showModal({
            title: '提示',
            content: res.data.message,
            showCancel: true
          })
        }
      })
    }
  },
  pays2(res2) {
    let that = this
    wx.requestPayment({
      timeStamp: res2.timeStamp,
      nonceStr: res2.nonceStr,
      package: res2.package_str,
      signType: res2.signType,
      paySign: res2.paySign,
      success(res) {
        that.data.bok = true
        wx.hideLoading()
        wx.showToast({
          title: '支付成功',
          mask: "true"
        })
        wx.navigateTo({
          url: '/pages/paystatus/success/success?money=' + that.data.total + "&text=" + "消费" + "&orderNumber=" + that.data.orderNumber + "&plateNumber=" + that.data.plateNumber + '&couponnum=' + that.data.couponnum + '&serviceCardnum=' + that.data.serviceCardnum
        })
      },
      fail(res) {
        that.data.bok = true
        wx.showModal({
          title: '提示',
          content: '支付失败，是否重新支付',
          success: function(res) {
            if (res.confirm) {
              that.pays2(res2)
              console.log(that.data.bottomMoney, "123")
              wx.hideLoading()
            } else if (res.cancel) {
              // console, log(that.data.bottomMoney, "123")
            }
          }
        })
        wx.hideLoading()
      }
    })
  },
  onShow: function() {
    //  判断是否选择了不适用优惠券
    this.getMemberInfoByOpenId()
    console.log(this.data.getcoupnPrevent, this.data.selectCouponList, "判断是否选择了不适用优惠券")
    if (this.data.getcoupnPrevent == 1) {
      console.log("input Bok")
      this.inputOk()
      this.data.getcoupnPrevent = 0
      return false
    }
    if (this.data.selectCouponList.length > 0) {
      // 判断活动是否可与优惠券叠加
      if (this.data.activitySuperposition == '0') {
        // 不可叠加 清除直降和折扣的金额
        console.log(this.data.bottomMoney, "下一步")
        this.data.bottomMoney = 0
        this.setData({
          couponmoney: 0,
          zhemoney: 0,
          jianmoney: 0
        })
      }
      // 判断如果有优惠券 就清空这个优惠券的信息
      if (this.data.couponPageMoney) {
        console.log(this.data.bottomMoney, this.data.couponPageMoney, "看看这是啥南北")
        this.data.bottomMoney = math.minus(this.data.bottomMoney, this.data.couponPageMoney)
        this.setData({
          bottomMoney: this.data.bottomMoney,
          couponPageMoney: "",
          couponIds: "",
        })
      }
      //转换为数字 
      let prices = 0
      let couponids = []
      this.data.selectCouponList.map((item, index) => {
        item.preAmount = Number(item.preAmount)
        prices = math.plus(item.preAmount, prices)
        couponids.push(item.id);
      })
      console.log(this.data.bottomMoney, prices, "看看这是啥")
      this.data.bottomMoney = math.plus(this.data.bottomMoney, prices).toFixed(2)
      this.data.total = math.minus(this.data.plateNumber, this.data.bottomMoney).toFixed(2)
      this.setData({
        total: this.data.total,
        couponPageMoney: prices,
        couponIds: couponids,
        bottomMoney: this.data.bottomMoney,
        couponState: true
      })
    } else {
      if (this.data.getcoupnPrevent == 1) {
        this.data.total = math.minus(this.data.plateNumber, this.data.bottomMoney)
        this.setData({
          total: this.data.total,
          couponPageMoney: "",
          couponIds: "",
          bottomMoney: this.data.bottomMoney,
        })
      }
    }
  },
  // 获取游客加油次数
  getCountByOpenId() {
    let openid = wx.getStorageSync("openid")
    console.log(this.data.params, "撒旦发顺丰撒旦飞洒发的")
    api.getCountByOpenId({
      openid: openid
    }).then(res => {
      console.log(res.data.result,"游客加油次数")

      if (res.data.result>= 1) {
        if (this.data.params) {
          wx.reLaunch({
            url: '/pages/login/login?page=pages/nextY/nextY&scene=' + this.data.scene + '&state=1',
          })
        } else {
          wx.reLaunch({
            url: '/pages/login/login?page=pages/nextY/nextY&' + this.data.scene + '&state=1',
          })
        }
      }
    })
  },
  // 根据openid 查询会员信息
  getMemberInfoByOpenId(){
    let openid = wx.getStorageSync('openid')
    api.getMemberInfoByOpenId({openid}).then(res=>{
      console.log(res.data.data,"根据openid查询会员信息")
      if(res.data.data){
        wx.setStorageSync('userInfo', res.data.data)
      }else{
        this.data.userInfo = null
        wx.removeStorageSync('userInfo')
        wx.removeStorageSync('jwt')
        this.getCountByOpenId()
      }
    })
  }
})